<?php

declare(strict_types=1);

namespace App\Chat\Controller;

use App\Chat\Annotation\ChatAuth;
use App\CustHub\Request\CorpTagRequest;
use App\CustHub\Service\ChCorpTagService;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: 'ch/chat/tag'),ChatAuth]
class WxTagController extends ChatBaseController
{
    #[Inject]
    protected ChCorpTagService $service;

    /**
     * 同步个人标签
     * @param CorpTagRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('syncTag')]
    public function syncTag(CorpTagRequest $request): ResponseInterface
    {
        $data = $request->all();
        $data['main_uid'] = Context::get('main_uid');
        return $this->service->syncPersonalTag($data) ? $this->success('标签同步中，请稍后查看') : $this->error('标签同步失败');
    }

    /**
     * 新增个人标签
     * @param CorpTagRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('save')]
    public function save(CorpTagRequest $request): ResponseInterface
    {
        $data = $request->all();
        $data['main_uid'] = Context::get('main_uid');
        return $this->service->save($data) ? $this->success() : $this->error();
    }

    /**
     * 编辑个人标签
     * @param CorpTagRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('update')]
    public function update(CorpTagRequest $request): ResponseInterface
    {
        $data = $request->all();
        $data['main_uid'] = Context::get('main_uid');
        return $this->service->updateTag($data) ? $this->success() : $this->error();
    }

    /**
     * 删除个人标签
     * @param CorpTagRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('delete')]
    public function delete(CorpTagRequest $request): ResponseInterface
    {
        return $this->service->deleteTag($request->all()) ? $this->success() : $this->error();
    }
}